package com.example.demo.login;

import com.example.demo.Service.AdminlengService;
import com.example.demo.Service.BlacklistService;
import com.example.demo.Switch.GroupSwitchController;
import com.example.demo.Switch.PublicApiController;
import love.forte.common.ioc.annotation.Beans;
import love.forte.common.ioc.annotation.Depend;
import love.forte.simbot.annotation.Filter;
import love.forte.simbot.annotation.OnGroup;
import love.forte.simbot.annotation.OnGroupMsgRecall;
import love.forte.simbot.api.message.MessageContent;
import love.forte.simbot.api.message.MessageContentBuilder;
import love.forte.simbot.api.message.MessageContentBuilderFactory;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.message.events.GroupMsgRecall;
import love.forte.simbot.api.sender.Sender;
import love.forte.simbot.filter.MatchType;
import net.mamoe.mirai.contact.BotIsBeingMutedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Beans
@Component
public class GroupTake extends Thread{

    @Autowired
    private BlacklistService test;
    @Autowired
    private GroupSwitchController groupSwitchController;
    @Autowired
    private AdminlengService adminlengService;
    @Autowired
    private PublicApiController publicApiController;

    private Boolean flag = true;

    @OnGroup
    @Filter(value = "/", matchType = MatchType.STARTS_WITH)
    public void group(GroupMsg msg, Sender sender) {
        String json=groupSwitchController.groupSwitch(msg);
        if(json!=null&&flag&&test.selectlist(msg.getAccountInfo().getAccountCode()))
        {
            sender.sendGroupMsg(msg.getGroupInfo().getGroupCode(),json);
        }
    }
    @OnGroup
    @Filter(value = "#", matchType = MatchType.STARTS_WITH)
    public void API(GroupMsg msg, Sender sender) {
        sender.sendGroupMsg(msg.getGroupInfo(),publicApiController.apiSwitch(msg));
    }
    @OnGroup
    @Filter(value = "循环", matchType = MatchType.STARTS_WITH)
    public void forTake(GroupMsg msg, Sender sender) {
        String[] num=msg.getMsg().split(" ");
        String json=groupSwitchController.userTake(num[1]);
        if(flag&&test.selectlist(msg.getAccountInfo().getAccountCode()))
        {
            if(json==null)
            {
                for (int i = 0; i < Integer.parseInt(num[2]); i++) {
                    sender.sendGroupMsg(msg.getGroupInfo(),num[1]);
                }
            }
            else if(!(num.length<3)) {
                for (int i = 0; i < Integer.parseInt(num[2]); i++) {
                    sender.sendGroupMsg(msg.getGroupInfo(),json);
                }
            }else
            {
                sender.sendGroupMsg(msg.getGroupInfo(),json);
            }
        }
    }
    /*@OnGroup
    @Filter(value = "涩图", matchType = MatchType.STARTS_WITH)
    public void sex(GroupMsg msg, Sender sender) {
        String json= msg.getText();
        String[] num=json.split(" ");
        if(num.length>=2) {
            for (int i = 0; i < Integer.parseInt(num[1]); i++) {
                try {
                    sleep(3000);
                    sender.sendGroupMsg(msg.getGroupInfo(),"/setu -root passwd=caigouzi");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }else
        {
            sender.sendGroupMsg(msg.getGroupInfo(),"/setu -root passwd=caigouzi");
        }
    }*/
    @OnGroup
    @Filter(value = "^((?!/).)*$", matchType = MatchType.REGEX_FIND)
    public void take(GroupMsg msg, Sender sender) {
        String json=groupSwitchController.userTake(msg);
        if(null!=json&&flag&&test.selectlist(msg.getAccountInfo().getAccountCode()))
        {
            try {
                sender.sendGroupMsg(msg.getGroupInfo().getGroupCode(),json);
            }catch (BotIsBeingMutedException bot)
            {
                System.out.println("群送信器出现异常："+bot.getMessage()+ System.currentTimeMillis());
            }
        }
    }
    @OnGroup
    @Filter(atBot = true)
    public void atbot(GroupMsg msg, Sender sender) {
        if(flag)
        {
            sender.sendGroupMsg(msg.getGroupInfo(), "输入'帮助'可以查看小结巴功能");
        }
        else
        {
            sender.sendGroupMsg(msg.getGroupInfo(), "小结巴处于被关闭状态，有需要请联系主人");
        }
    }

    @OnGroup
    @Filter(value = "开启小结巴", matchType = MatchType.EQUALS)
    public void oppen(GroupMsg msg, Sender sender) {
        if (test.selectrole(msg.getAccountInfo().getAccountCode())) {
            flag = true;
            sender.sendGroupMsg(msg.getGroupInfo(), "开启成功");
        }
    }

    @OnGroup
    @Filter(value = "关闭小结巴", matchType = MatchType.EQUALS)
    public void remove(GroupMsg msg, Sender sender) {
        if (test.selectrole(msg.getAccountInfo().getAccountCode()))
        {
            flag = false;
            sender.sendGroupMsg(msg.getGroupInfo(), "关闭成功");
        }
    }

    @OnGroup
    @Filter(value = "黑名单", matchType = MatchType.REGEX_FIND)
    public void black(GroupMsg msg, Sender sender)
    {
        if (test.selectrole(msg.getAccountInfo().getAccountCode())) {
            String[] arr=msg.getMsg().split(" ");
            test.insertUser(arr[1]);
            sender.sendGroupMsg(msg.getGroupInfo(), "关闭成功");
        }
    }
    //群消息撤回事件
    @OnGroupMsgRecall
    public void groupcall(GroupMsgRecall recall, Sender sender)
    {
        try {
            sender.sendGroupMsg(recall.getGroupInfo(), adminlengService.groupcall());
        }catch (Exception ignored)
        {
        }

    }

    /*@Autowired
    MessageContentBuilderFactory builderFactory;

    @OnGroup
    public void test(Sender sender)
    {

        MessageContentBuilder builder = builderFactory.getMessageContentBuilder();
        builder.image("C:\\Users\\xiaomi\\Pictures\\_{~Q%$R2QKH5])YGUD~@QVM.jpg");
        // do build...
        MessageContent msg = builder.build();
        // 发送消息举例：发送私聊消息
        // 直接使用消息实例发送。
        System.out.println(msg.getMsg());
        sender.sendGroupMsg("814789068", msg);
    }*/
}
